package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;

/**
 * Created by Administrator on 2016/1/13.
 */
public class MallGridview2 extends GridView {




    public MallGridview2(Context context) {
        super(context);
    }

    public MallGridview2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MallGridview2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    /**
     * listview嵌套GridView时需重写
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        // 父容器传过来的宽度的值
        int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft()
                - getPaddingRight();
        // 父容器传过来的高度的值
        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingLeft()
                - getPaddingRight();
        int height2 = getMeasuredHeight();
        int count = getCount();
        int childCount = getChildCount();
        int measuredHeight=0;
        int rowcount=1;
        int numColumns = getNumColumns();
        if(childCount>0) {
             measuredHeight = getChildAt(0).getMeasuredHeight();
            int rcount = count / numColumns;
            rowcount=  numColumns>count ?1:(count % numColumns) ==0?rcount:rcount+1;
        }
        if (widthMode == MeasureSpec.EXACTLY
                 && measuredHeight != 0) {
            height = measuredHeight*rowcount;
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                    MeasureSpec.EXACTLY);
        } else if (widthMode != MeasureSpec.EXACTLY
                && heightMode == MeasureSpec.EXACTLY && measuredHeight != 0.0f) {

            widthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
                    MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public int measureDimension(int defaultSize,int measureSpace){
        int result;
        int specMode=MeasureSpec.getMode(measureSpace);
        int specSize=MeasureSpec.getSize(measureSpace);
        if(specMode==MeasureSpec.EXACTLY) {
            result=specSize;
        }else{
            result=defaultSize;
            if(specMode==MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
         switch (ev.getAction()){
                     case MotionEvent.ACTION_MOVE:
                         return true;
                 }

        return super.onTouchEvent(ev);
    }
}
